package com.wsz.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wsz.system.custom.CustomUser;
import com.wsz.system.pojo.User;
import com.wsz.system.service.UserService;
import com.wsz.vo.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Collections;

//创建方法根据用户名查询用户信息
@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",s);
        User one = userService.getOne(queryWrapper);
        if(one == null){
            throw new UsernameNotFoundException("用户不存在");
        }

        if(one.getStatus() == 0){
            throw new RuntimeException("用户已被封禁");
        }

        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(one,sysUser);

        return new CustomUser(sysUser, Collections.emptyList());
    }
}
